﻿using PHMEE.Entities.DaqInfo.TdDevices;
using PHMEE.IRepositories.DaqInfo.TdDevices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;

namespace PHMEE
{
    public class DaqInfoDataSeed : IDataSeedContributor, ITransientDependency
    {
        private IGuidGenerator _guidGenerator;
        private readonly IRepository<Category, Guid> _categoryRepository;
        private readonly IRepository<TdDatabase, Guid> _tdDatabaseRepository;
        private readonly ITdDeviceTypeRepository _tdDeviceTypeRepository;
        private readonly ITdDeviceAttributeRepository _tdDeviceAttributeRepository;
        private readonly ITdDeviceInstanceRepository _tdDeviceInstanceRepository;
        private readonly ITdDeviceManager _tdDeviceManager;
        public DaqInfoDataSeed(
            IGuidGenerator guidGenerator,
            IRepository<Category, Guid> categoryRepository,
            IRepository<TdDatabase, Guid> tdDatabaseRepository,
            ITdDeviceTypeRepository tdDeviceTypeRepository,
            ITdDeviceAttributeRepository tdDeviceAttributeRepository,
            ITdDeviceInstanceRepository tdDeviceInstanceRepository)
        {
            _guidGenerator = guidGenerator;
            _categoryRepository = categoryRepository;
            _tdDatabaseRepository = tdDatabaseRepository;
            _tdDeviceTypeRepository = tdDeviceTypeRepository;
            _tdDeviceAttributeRepository = tdDeviceAttributeRepository;
            _tdDeviceInstanceRepository = tdDeviceInstanceRepository;
        }
        public async Task SeedAsync(DataSeedContext context)
        {
            //生成采集定义种子数据
            if (await _categoryRepository.CountAsync() == 0)
            {
                var SpeedEffectiveValues = new Category { Name = "速度有效值", OrderBy = 0, LimitPara = LimitParaEnum.EffectiveSpeed };
                var AccelerationPeaks = new Category { Name = "加速度峰值", OrderBy = 1, LimitPara = LimitParaEnum.PeakAcceleration };
                var AccEffective = new Category { Name = "加速度有效值", OrderBy = 2, LimitPara = LimitParaEnum.EffectiveAcc };
                var Temperatures = new Category { Name = "温度", OrderBy = 3, LimitPara = LimitParaEnum.Temperature };
                await _categoryRepository.InsertManyAsync(new[] { SpeedEffectiveValues, AccelerationPeaks, AccEffective, Temperatures });
            }
            ////生成TD数据库的种子数据
            //// Step 1: 创建数据库
            //if (await _tdDatabaseRepository.CountAsync() == 0)
            //{
            //    var databaseId = _guidGenerator.Create();
            //    var database = new TdDatabase(databaseId)
            //    {
            //        Name = "ee_c0261e5939a24086b974e7c9e1b93d7c",
            //        Description = "Mastic能效数据库"
            //    };
            //    await _tdDatabaseRepository.InsertAsync(database);

            //    // Step 2: 创建超级表
            //    var superSpmTableId = _guidGenerator.Create();
            //    var superGpsTableId = _guidGenerator.Create();
            //    var superFmTableId = _guidGenerator.Create();

            //    var superSpmTable = new TdDeviceType(superSpmTableId)
            //    {
            //        Name = "shaft_power_meter",
            //        Description = "轴功率计设备",
            //        DatabaseId = databaseId
            //    };
            //    var superGpsTable = new TdDeviceType(superGpsTableId)
            //    {
            //        Name = "gps",
            //        Description = "GPS设备",
            //        DatabaseId = databaseId
            //    };
            //    var superFmTable = new TdDeviceType(superFmTableId)
            //    {
            //        Name = "flowmeter",
            //        Description = "流量计设备",
            //        DatabaseId = databaseId
            //    };
            //    await _tdDeviceTypeRepository.InsertAsync(superSpmTable);
            //    await _tdDeviceTypeRepository.InsertAsync(superGpsTable);
            //    await _tdDeviceTypeRepository.InsertAsync(superFmTable);

            //    // Step 3: 超级表添加相应的属性
            //    var attributes = new List<TdDeviceAttribute>
            //    {
            //        //轴功率计
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "ts", Description = "时间戳", ColumnType = "TIMESTAMP", TdDeviceTypeId = superSpmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "speed", Description = "轴转速", ColumnType = "FLOAT", TdDeviceTypeId = superSpmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "power", Description = "轴功率", ColumnType = "FLOAT", TdDeviceTypeId = superSpmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "torque", Description = "轴扭矩", ColumnType = "FLOAT", TdDeviceTypeId = superSpmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "total_revs", Description = "总转数", ColumnType = "BIGINT UNSIGNED", TdDeviceTypeId = superSpmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "total_eng", Description = "总能量", ColumnType = "BIGINT UNSIGNED", TdDeviceTypeId = superSpmTableId },
            //        //GPS
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "ts", Description = "时间戳", ColumnType = "TIMESTAMP", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "latitude", Description = "纬度", ColumnType = "FLOAT", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "longitude", Description = "经度", ColumnType = "FLOAT", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "ground_speed", Description = "对地速度", ColumnType = "FLOAT", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "distance", Description = "航程", ColumnType = "FLOAT", TdDeviceTypeId = superGpsTableId },
            //        //流量计
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "ts", Description = "时间戳", ColumnType = "TIMESTAMP", TdDeviceTypeId = superFmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "total_value", Description = "总流量", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "oil_value", Description = "分钟油耗", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "instant", Description = "即时油耗", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "density", Description = "密度", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "temperature", Description = "温度", ColumnType = "FLOAT", TdDeviceTypeId = superFmTableId },
            //        new TdDeviceAttribute(_guidGenerator.Create()) { Name = "fuel_type", Description = "油品", ColumnType = "INT", TdDeviceTypeId = superFmTableId }
            //    };
            //    await _tdDeviceAttributeRepository.InsertManyAsync(attributes);
            //    // Step 4: 创建子表
            //    var subDeviceInstances = new List<TdDeviceInstance>
            //    {
            //        new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "spm_001", Description = "1#轴功率计", TdDeviceTypeId = superSpmTableId },
            //        new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "gps_001", Description = "1#gps", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_001", Description = "主副机进油", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_002", Description = "主副机回油", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_003", Description = "副机进油", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_004", Description = "副机回油", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_005", Description = "锅炉进油", TdDeviceTypeId = superGpsTableId },
            //        new TdDeviceInstance(_guidGenerator.Create()) { InstanceName = "fm_006", Description = "锅炉回油", TdDeviceTypeId = superGpsTableId }
            //    };
            //    await _tdDeviceInstanceRepository.InsertManyAsync(subDeviceInstances);
            //}
        }
    }
}
